const DAY_TIME = 86400000 // 一天的时间
 
const dateShift = (time) => new Date(new Date().getTime() - time * DAY_TIME) // 控制时间在多少天前/后
 
const monthDayShift = (monthNum, dayNum) => new Date(
        new Date(
            new Date().setMonth(
                new Date().getMonth() - monthNum
            )
        ).setDate(dayNum)
    ) // 控制月/日
 
const yesterdayTime = dateShift(1) // 昨天
const nowTime = dateShift(0) // 昨天
const beforeYesterdayTime = dateShift(2) // 前天
const beforeSevenDays = dateShift(7) // 七天前
const beforeThirtyDays = dateShift(30) // 三十天前
const tomorrowTime = dateShift(-1) // 昨天
const afterTomorrowTime = dateShift(-2) // 前天
const afterSevenDay = dateShift(-7) // 七天前
const afterThirtyDays = dateShift(-30) // 三十天前
 
const beforeWeekFirstDay = monthDayShift(0, new Date().getDate() - (new Date().getDay() || 7) - 6) // 上周第一天
const thisWeekFirstDay = monthDayShift(0, new Date().getDate() - (new Date().getDay() || 7) + 1) // 本周第一天
const afterWeekFirstDay = monthDayShift(0, new Date().getDate() - (new Date().getDay() || 7) + 8) // 下周第一天
 
const beforeWeekLastDay = monthDayShift(0, new Date().getDate() - (new Date().getDay() || 7)) // 上周最后一天
const thisWeekLastDay = monthDayShift(0, new Date().getDate() - (new Date().getDay() || 7) + 7) // 本周最后一天
const afterWeekLastDay = monthDayShift(0, new Date().getDate() - (new Date().getDay() || 7) + 14) // 下周最后一天
 
const beforeHalfYearFirstDay = monthDayShift(6, 1) // 前半年的第一天
const beforeThirdMonthFirstDay = monthDayShift(3, 1) // 前三个月的第一天
const beforeTwoMonthFirstDay = monthDayShift(2, 1) // 前两个月的第一天
const beforeMonthFirstDay = monthDayShift(1, 1) // 上月的第一天
const thisMonthFirstDay = monthDayShift(0, 1) // 本月月的第一天
const afterMonthFirstDay = monthDayShift(-1, 1) // 下个月的第一天
 
const beforeHalfYearLastDay = monthDayShift(5, 0) // 半年前的最后一天
const beforeThirdMonthLastDay = monthDayShift(2, 0) // 上三个月的最后一天
const beforeTwoMonthLastDay = monthDayShift(1, 0) // 上两个月的最后一天
const beforeMonthLastDay = monthDayShift(0, 0) // 上月的最后一天
const thisMonthLastDay = monthDayShift(-1, 0) // 本月的最后一天
const afterMonthLastDay = monthDayShift(-2, 0) // 下个月的最后一天
 function addZero (str) {
	        let num
	        str >= 10 ? num = str : num = `0${str}`
	        return num
}
const dateUtils = {
    timeFun ({
                 date = new Date(),
                 ymr = false, // 无符号年月日
                 ymrSign = true, // 有符号年月日
                 time = false, // 无符号时分秒
                 timeSign = false, // 有符号时分秒
                 allTime = false, // 年月日时分秒
                 allTimeSign = false, // 有符号年月日时分秒
                 allTimeWeek = false, // 有符号年月日时分秒、星期
                 dateSigns = '-', // 年月日符号
                 timeSigns = ':', // 时分秒符号
                 timeFormat = false // 时间格式化
             }) {
        const year = date.getFullYear().toString()
        // const month = date.getMonth() + 1
        // const todayDate = date.getDate()
        //
        // const hours = timeFormat ? date.setHours(0) : date.getHours()
        // const minutes = timeFormat ? date.setMinutes(0) : date.getMinutes()
        // const seconds = timeFormat ? date.setSeconds(0) : date.getSeconds()
        // const milliseconds = timeFormat ? date.setMilliseconds(0) : date.getMilliseconds()
        const week = date.getDay()
 
        const addZeroMonth = addZero(date.getMonth() + 1)
        const addZeroDate = addZero(date.getDate()).toString()
        const addZeroHours = addZero(date.getHours()).toString()
        const addZeroMinutes = addZero(date.getMinutes()).toString()
        const addZeroSeconds = addZero(date.getSeconds()).toString()
 
        const dateStr = year + addZeroMonth + addZeroDate
        const timeStr = addZeroHours + addZeroMinutes + addZeroSeconds
        const dateHaveSign = year + dateSigns + addZeroMonth + dateSigns + addZeroDate
        const timeHaveSign = addZeroHours + timeSigns + addZeroMinutes + timeSigns + addZeroSeconds
        if (ymr) return dateStr
        if (ymrSign) return dateHaveSign
        if (time) return timeStr
        if (timeSign) return timeHaveSign
        if (allTime) return dateStr + timeStr
        if (allTimeWeek) return dateHaveSign + ' ' + timeHaveSign + ' ' + this.weekStr(week)
        if (allTimeSign) return dateHaveSign + ' ' + timeHaveSign
    },
    beforeWeekFirst (params) { // 上周第一天
        return this.timeFun({date: beforeWeekFirstDay, ...params})
    },
	
    thisWeekFirst (params) { // 这周第一天
        return this.timeFun({date: thisWeekFirstDay, ...params})
    },
    afterWeekFirst (params) { // 下周第一天
        return this.timeFun({date: afterWeekFirstDay, ...params})
    },
    beforeWeekLast (params) { // 上周最后一天
        return this.timeFun({date: beforeWeekLastDay, ...params})
    },
    thisWeekLast (params) { // 这周最后一天
        return this.timeFun({date: thisWeekLastDay, ...params})
    },
    afterWeekLast (params) { // 下周最后一天
        return this.timeFun({date: afterWeekLastDay, ...params})
    },
    beforeHalfYearFirst (params) { // 半年前第一天
        return this.timeFun({date: beforeHalfYearFirstDay, ...params})
    },
    beforeThirdMonthFirst (params) { // 三个月前第一天
        return this.timeFun({date: beforeThirdMonthFirstDay, ...params})
    },
    beforeTwoMonthFirst (params) { // 两个月前第一天
        return this.timeFun({date: beforeTwoMonthFirstDay, ...params})
    },
    beforeMonthFirst (params) { // 一个月前第一天
        return this.timeFun({date: beforeMonthFirstDay, ...params})
    },
    thisMonthFirst (params) { // 本月第一天
        return this.timeFun({date: thisMonthFirstDay, ...params})
    },
    afterMonthFirst (params) { // 下个月第一天
        return this.timeFun({date: afterMonthFirstDay, ...params})
    },
    beforeHalfYearLast (params) { // 半年前最后一天
        return this.timeFun({date: beforeHalfYearLastDay, ...params})
    },
    beforeThirdMonthLast (params) { // 三个月前最后一天
        return this.timeFun({date: beforeThirdMonthLastDay, ...params})
    },
    beforeTwoMonthLast (params) { // 两个月前最后一天
        return this.timeFun({date: beforeTwoMonthLastDay, ...params})
    },
    beforeMonthLast (params) { // 一个月前最后一天
        return this.timeFun({date: beforeMonthLastDay, ...params})
    },
    thisMonthLast (params) { // 本月最后一天
        return this.timeFun({date: thisMonthLastDay, ...params})
    },
    afterMonthLast (params) { // 下个月最后一天
        return this.timeFun({date: afterMonthLastDay, ...params})
    },
    tomorrow (params) { // 明天
        return this.timeFun({date: tomorrowTime, ...params})
    },
    afterTomorrow (params) { // 后天
        return this.timeFun({date: afterTomorrowTime, ...params})
    },
    afterSeven (params) { // 七天后
        return this.timeFun({date: afterSevenDay, ...params})
    },
    afterThirty (params) { // 三十天后
        return this.timeFun({date: afterThirtyDays, ...params})
    },
    yesterday (params) { // 昨天
        return this.timeFun({date: yesterdayTime, ...params})
    },
    beforeYesterday (params) { // 前天
        return this.timeFun({date: beforeYesterdayTime, ...params})
    },
    beforeSeven (params) { // 七天前
        return this.timeFun({date: beforeSevenDays, ...params})
    },
    beforeThirty (params) { // 三十天前
        return this.timeFun({date: beforeThirtyDays, ...params})
    },
	nowTime(params){
		console.log(params)
			return this.timeFun({date:nowTime,...params});
	},
    weekStr (week, weekText = '星期') { // 星期转换
        if (week === 0) week = weekText + '日'
        if (week === 1) week = weekText + '一'
        if (week === 2) week = weekText + '二'
        if (week === 3) week = weekText + '三'
        if (week === 4) week = weekText + '四'
        if (week === 5) week = weekText + '五'
        if (week === 6) week = weekText + '六'
        return week
    },
	diffDays(startTime,endTime){
		let s1 = new Date(startTime.replace(/-/g, "/"));
		let s2 = new Date(endTime.replace(/-/g, "/"));//当前日期：2017-04-24
		let days = s2.getTime() - s1.getTime();
		
		
		let in_days = days / (1000 * 60 * 60 * 24);
		if(in_days < 1){
			in_days = Math.ceil(in_days);
		}else{
			in_days = Math.floor(in_days);
		}
		var time = this.timeFun({date:nowTime});
		//判断是不是今天
		if(time== startTime){
			//判断当前时间
			var bow_time = this.timeFun({
						date:nowTime,
						ymrSign:false,
						allTimeSign:true,
				});
			var bo_time_date = new Date(bow_time.replace(/-/g, "/"));
			let sixTime = new Date(time.replace(/-/g, "/"));
			var six_time_getTime = sixTime.getTime()+3600000*6;
			console.log(bow_time)
			if(bo_time_date.getTime() < six_time_getTime){
				in_days += 1;
			}
			
		}
		return in_days;
	},
	timeFormat(time,fmt){
		var date = new Date(time*1000);
		 let ret;
		     const opt = {
		         "Y+": date.getFullYear().toString(),        // 年
		         "m+": (date.getMonth() + 1).toString(),     // 月
		         "d+": date.getDate().toString(),            // 日
		         "H+": date.getHours().toString(),           // 时
		         "M+": date.getMinutes().toString(),         // 分
		         "S+": date.getSeconds().toString()          // 秒
		         // 有其他格式化字符需求可以继续添加，必须转化成字符串
		     };
		     for (let k in opt) {
		         ret = new RegExp("(" + k + ")").exec(fmt);
		         if (ret) {
		             fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
		         };
		     };
			 console.log(date.getMonth())
		     return fmt;
	},
	timeSecond(time){
		var date = new Date(time);
		 
		return date.getTime();
	},
	conutDown(time){
	      var nowTime = new Date();//返回当前时间总的毫秒数
		  
		  let nowTimerSt = new Date(time);
		  
	      //times是剩余时间总的秒数
	      var times=(+nowTimerSt - nowTime) / 1000;
	       
	       var d=parseInt(times / 60 /60 /24);//天
	       var h=parseInt(times / 60 /60);//时
	       var m=parseInt(times / 60 % 60);//分
	       var s=parseInt(times % 60 ); //秒
	       if(h<0){
			   h = 0;
		   }
		   if(m<0){
			   m = 0;
		   }
		   if(s<0){
			   s = 0
		   }
	       
	       return {
			   'h':h,
			   'm':m,
			   's':s
		   };
	     },
		 
}
 
export {dateUtils}